package require Mk4tcl
package require sqlite3 3.6.19
package require tdbc::sqlite3

mk::file open odb docs/wubchain.tkd

tdbc::sqlite3::connection create ndb docs/wubchain.sqlite

set statement("insert_user") [ndb prepare {INSERT INTO users (username, password, email, sid, last_login, emoticons, nick_colors, lines,
								       auto_update, user_list, mobile, from_language, to_language, time_stamps, time_zone,
								       hide_logon_logoff, admin, state, ipaddr, port, user_agent) 
    VALUES (:username, :password, :email, :sid, :last_login, :emoticons, :nick_colors, :lines,
	    :auto_update, :user_list, :mobile, :from_language, :to_language, :time_stamps, :time_zone,
	    :hide_logon_logoff, :admin, :state, :ipaddr, :port, :user_agent)}]
set statement("insert_subject") [ndb prepare {INSERT INTO user_subjects (username, id, subject) VALUES (:username, :id, :subject)}]


mk::loop i odb.users {
    foreach {k v} [mk::get $i] { set $k $v }
    $statement("insert_user") execute
    set n 0
    mk::loop j $i.subjects {
	foreach {k v} [mk::get $j] { set $k $v}
	set id $n
	$statement("insert_subject") execute
	incr n
    }
}

ndb close
mk::file close odb
exit
